package com.magicodex.bookkeeping.ui.statistic;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.magicodex.bookkeeping.R;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.TextViewUtil;
import com.magicodex.bookkeeping.biz.statistic.dto.StatisticDTO;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author mh.z
 */
public class StatisticListViewAdapter extends BaseAdapter {
    private Activity activity;
    private List<StatisticDTO> records;
    /** 最后点击位置 */
    private int lastClickPosition;

    public StatisticListViewAdapter(Activity activity, List<StatisticDTO> records) {
        this.activity = activity;
        this.records = records;
        this.lastClickPosition = -1;
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int i) {
        return records.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public List<StatisticDTO> getRecords() {
        return records;
    }

    public void setRecords(List<StatisticDTO> records) {
        this.records = records;
    }

    public int getLastClickPosition() {
        return lastClickPosition;
    }

    public void setLastClickPosition(int lastClickPosition) {
        this.lastClickPosition = lastClickPosition;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        StatisticListViewHolder holder;

        if (view == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            view = inflater.inflate(R.layout.statistic_item, null);
            holder = createHolder(view);

            view.setTag(holder);
        } else {
            holder = (StatisticListViewHolder) view.getTag();
        }

        StatisticDTO record = records.get(i);
        // 填充数据
        populateData(holder, record);

        return view;
    }

    /**
     *
     * @param view
     * @return
     */
    protected StatisticListViewHolder createHolder(View view) {
        CheckUtil.notNull(view, "view null");

        // 账户类别
        TextView accountCategory = view.findViewById(R.id.text_account_category);
        // 汇总金额
        TextView totalMoney = view.findViewById(R.id.text_total_money);

        return new StatisticListViewHolder(accountCategory, totalMoney);
    }

    /**
     * 填充数据
     *
     * @param holder
     * @param record
     */
    protected void populateData(StatisticListViewHolder holder, StatisticDTO record) {
        CheckUtil.notNull(holder, "holder null");
        CheckUtil.notNull(record, "record null");

        // 账户类别
        String accountCategory = record.getAccountCategory();
        TextView accountCategoryView = holder.getAccountCategory();
        TextViewUtil.setString(accountCategoryView, accountCategory);
        // 汇总金额
        BigDecimal totalMoney = record.getTotalMoney();
        TextView totalMoneyView = holder.getTotalMoney();
        TextViewUtil.setBigDecimalWithThousandsFmt(totalMoneyView, totalMoney);
    }

}
